<?php
class Reviews extends Controller {
    
    private $imgdata = array(
    'dir' => array(
    'original' => 'images/uploads/original/',
    'thumb' => 'images/uploads/thumb/',
    'large'=>'images/uploads/large/'
    ),
    'total' => 0,
    'images' => array(),
    'error' => ''
    );
    
    public function index() {
        $this->load->model('reviews_model');
        $this->load->library("pagination");
        $config = array();
        $config["base_url"] = base_url() ."secu/reviews/reviewslist/";
        $config["total_rows"] = $this->reviews_model->list_reviews_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 4;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $data["partnerlang"]=$this->config->item("partnerlang");
        $data["reviews_arr"] = $this->reviews_model->list_reviews($config["per_page"], $page);
        $data["pagination"] = $this->pagination->create_links();
        //$data["reviews_lang"] = $this->reviews_model->get_reviews_lang();
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Reviews',base_url()."secu/reviews/");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();  
        $this->template->load('admintemplate','reviews/reviewslist',$data);

    }
    
    public function managereviews(){
        $partnerlang=$this->config->item('partnerlang');
        $reviews_id=  $this->uri->segment(5);
        $del_image_status= $this->uri->segment(7);
        $del_image= $this->uri->segment(6);
        if($del_image){
            $data['image_deleted']="Image {$del_image} deleted {$del_image_status}";
        }
        $this->load->model("reviews_model");
        if(!empty($reviews_id)){            
            $reviewsdetails=  $this->reviews_model->get_reviews_details_by_id($reviews_id);
            if($reviewsdetails){
                foreach ($partnerlang as $langkey => $langval) {
                    $tmptitle=  $this->reviews_model->get_reviews_details_lang_by_id($reviewsdetails[0]['title'], $langkey);
                    $reviewsdetails[0]["title".$langkey]=$tmptitle[0]['lvalue'];                    
                }
                /*echo "<pre>";
                print_r($reviewsdetails);
                echo "</pre>";*/
                $data['reviewsdetails']=$reviewsdetails[0];
            }            
        }        
        //print_r($hotel_arr);
        $data['partnerlang']=$partnerlang;
        
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Reviews',base_url()."secu/reviews/");
        $this->breadcrumb->addCrumb('Manage Reviews',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();          
        
        $this->template->set('controller', $this);
        $this->template->set('title', 'reviews');
        $this->template->load_partial('admintemplate', 'reviews/reviews', $data);
    }
    
    public function doaddreviews()
    {
        
        $partnerlang = $this->config->item("partnerlang");
        $this->load->model("reviews_model");
        $validate_arr_lang=array();
        $reviews_detail_arr_lang=array();
        foreach($partnerlang as $langkey=>$langval){
            $validate_arr_lang+=array(array( 'field' => 'title'.$langkey,'label' => 'Title-'.$langkey,'rules' => 'required')
                                     );
            $reviews_detail_arr_lang+=array("title".$langkey=>$this->input->post('title'.$langkey)
                                         );
        }
        //print_r($reviews_detail_arr_lang);
        $validate_reviews = array_merge($validate_arr_lang);

        $reviewsdetails = array_merge($reviews_detail_arr_lang, array(
        "status"  =>$this->input->post('status')
        ));
        if ($this->input->post('id')){
            $reviewsdetails=  array_merge(array("reviews_id"=>$this->input->post('id')), $reviewsdetails);
        }        
        //print_r($hotel_arr);
        $data['hotel_arr']=$hotel_arr;
        
        $this->session->set_userdata($reviewsdetails);
        $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
        $this->form_validation->set_rules($validate_reviews);
        
        $data['partnerlang']=$partnerlang;
        $data['reviewsdetails']=$reviewsdetails;
        //print_r($this->form_validation->run());exit;

        if ($this->form_validation->run() == FALSE) {
            //exit('dee');
            //print_r($reviewsdetails);
    //exit();
            //$this->template->load('admintemplate', 'admin/hotel',$data);
            $this->template->set('controller', $this);
            $this->template->set('title', "login");
            $this->template->load_partial('admintemplate', 'reviews/reviews',$data);
        } else  {
            
            //$this->template->set('product_header', 'Admin Home');
            $this->load->model('reviews_model');
            
            if ($this->input->post('id')){
            
                $this->reviews_model->updatereviews($reviewsdetails, $data);
                $reviews_id=$this->input->post('id');
                //exit('yyy');
            }else{                
                $reviews_id=$this->reviews_model->savereviews($reviewsdetails, $partnerlang);
            }            
            if($reviews_id>0){
                redirect(base_url() . 'secu/reviews/managereviews/id/'.$reviews_id);
            }
            redirect(base_url() . 'secu/reviews/');
        }


    }

    public function delreviews()
    {
        $reviews_id = $this->input->post("reviews_id");
        $this->load->model('reviews_model');
        $this->reviews_model->delete_reviews($reviews_id);
        redirect(base_url() . 'secu/reviews/reviewslist');
    }

    public function savereviewsimages()
     {
        $upload_from= $this->uri->segment(4);
        //exit($upload_from);
        $id=$this->input->post("id");
        $c_upload['upload_path']    = $this->imgdata['dir']['original'];
        $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
        $c_upload['max_size']       = '10000';
        $c_upload['max_width']      = '10000';
        $c_upload['max_height']     = '5000';
        $c_upload['remove_spaces']  = TRUE;
                
        $this->load->library('upload', $c_upload);

        if ($this->upload->do_upload()) {
//
            $img = $this->upload->data();
            $new_image = $this->imgdata['dir']['original'].trim($id)."_".trim(time())."_".$img['file_name'];
            $this->img_resize($img, $new_image,$id);
            
            unlink($this->imgdata['dir']['original'].$img['file_name']);
            //exit($this->imgdata['dir']['original'].$img['file_name']);
            $this->load->model('content');
            //exit();
            $this->content->saveimagestodb($id,trim($id)."_".trim(time())."_".$img['file_name'],$upload_from);



        } else {
            $this->imgerror= $this->upload->display_errors();            
        }
        if($upload_from=='reviewsimg'){
            redirect('secu/reviews/managereviews/id/'.$id);
        }else{
            redirect('admin/hotelcontent/?id='.trim($id)."#img");
        }

    }
    
    public function img_resize($upload,$new_image,$id)
    {
        $this->load->library('image_lib');
        $newpath = $new_image;

        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 595;
        $config['height']    = 320;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $newpath_thumb = $this->imgdata['dir']['thumb']."thumb_".trim($id)."_".trim(time())."_".$upload['file_name'];

        unset($config);
        $this->image_lib->clear();
         $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {

        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 100;
        $config['height']    = 80;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }
    
    public function deletereviewsimage()
    {
        $original_status=false;
        $thumb_status=false;
        $del_image_status="failed";
        $id=$this->uri->segment(7);
        $ori_img=$this->uri->segment(5); 
        $ext=  substr($ori_img, -3);        
        $ori_img=  substr($ori_img, 0, strlen($ori_img)-4).'.'.$ext;
        $this->load->model('content');
        $this->content->deleteimage($id, "reviewsimg");
        //
        //echo $this->imgdata['dir']['original'].$ori_img.'<br>';
        if (file_exists($this->imgdata['dir']['original'].$ori_img)){
            unlink($this->imgdata['dir']['original'].$ori_img);
            $original_status=true;
        }
        //echo $this->imgdata['dir']['thumb'].'thumb_'.$ori_img;
        if (file_exists($this->imgdata['dir']['thumb'].'thumb_'.$ori_img)){
            unlink($this->imgdata['dir']['thumb'].'thumb_'.$ori_img);
            $thumb_status=true;
        }
        //exit();
        if ($original_status && $thumb_status){
            $del_image_status="successfully";
        }elseif ($original_status || $thumb_status) {
            $del_image_status="partially";
        }
        redirect('secu/reviews/managereviews/id/'.$id.'/'.$ori_img.'/'.$del_image_status);
    }
}
?>